<form id="emember_form" method="post">
<?php include_once('membership_level_list_view.php');?>
</form>
<div id="hidden_content" style="display:none" >
<?php include_once('add_membership_level_view.php');?>
</div>
<script type="text/javascript">
    $j = jQuery.noConflict();    
   function checkExpiry(form){
      $j(form).find('input[id^=noexpire_button_]').
         each(function(i){
            $j(this).click(function(){               
                $j('#expire_on_fixed_date_new_level').attr('disabled','disabled');
                $j('#level_name_new_expire').attr('disabled','disabled');
                $j('#level_name_new_calendar').attr('disabled','disabled');
                if ($j(this).attr('id') == 'noexpire_button_interval_new_level') {
                    $j('#level_name_new_expire').removeAttr('disabled');
                    $j('#level_name_new_calendar').removeAttr('disabled');                    
                }                    
                else if ($j(this).attr('id') == 'noexpire_button_fixed_date_new_level'){
                    $j("#expire_on_fixed_date_new_level").dateinput({'format':'yyyy-mm-dd',selectors: true,yearRange:[-100,100]});
                    $j('#expire_on_fixed_date_new_level').removeAttr('disabled');
                }           
            })
         });
   }
   function showForm(){
      $j('#add_update').attr('name','add_new');
      $j('#add_update').val('Submit');
      var levelForm = $j($j('#hidden_content').html()).clone();
      $j('#hidden_content').html('');
      var levelList = $j($j('#emember_form').slideUp().html()).clone();
      $j('#emember_form').html('');
      $j(levelList).appendTo('#hidden_content');
      $j('#emember_form').append($j(levelForm)).slideDown();
      $j('#cancel_button').click(showList);
      $j('#level_name_msg').html('');
      $j("#level_name_new_allpages").attr("checked", "checked");
      $j("#level_name_new_allcategories").attr("checked", "checked");
      $j("#level_name_new_allposts").attr("checked", "checked");
      $j("#level_name_new_allcomments").attr("checked", "checked");
      checkExpiry('#emember_form');
      $j("#level_name_new_level").keyup(function(){
        var $j_this = this;
        var user_name = $j(this).val();
        if(user_name!="" &&(user_name.length>4))
        {
           available = false;
           var target_url = '<?php echo admin_url( "admin-ajax.php" ); ?>';
           $j.get(target_url ,{"event":"check_level_name","action":"check_level_name","alias":$j(this).val()},
                 function(data){
                     var msg_obj = $j("#level_name_msg");
                     if(!msg_obj.length){
                        msg_obj = $j('<span id= "level_name_msg"/>');
                        $j($j_this).after(msg_obj);
                     }
                     available = data.status_code;
                     if(data.status_code)msg_obj.css("color", "green").html(data.msg);
                     else msg_obj.css("color", "red").html(data.msg);
                  },
            "json");
         }
      });      
   }
   function bindActions(){
      $j('#e_membership_levels').find('a').each(function(i){
         var label = $j(this).html();
         if(label == 'Edit')
         {
            $j(this).click(function(){
               showForm();
               $j('<input name="wpm_levels[new_level][id]" id="hidden_id" type="text" />').
               val($j(this).attr('id')).hide().appendTo($j('#emember_form'));
               $j('#add_update').attr('name','update_info');
               $j('#add_update').val('Update Info');
               var id = $j(this).attr('id');
               $j('#level_name_new_level').val($j('#alias_'+id).html());
               var role = $j.trim($j('#role_'+id).html());
               var roleId = new Array();
               <?php
                   $jroles = new WP_Roles();
                   $jf = true;
                   foreach($jroles->role_names as $jkey =>$jvalue) 
                       echo 'roleId["' . $jvalue . '"] = "' .$jkey . '"' . "\n"; 
               ?>;
               $j('#level_name_new_role').val(roleId[role]);
               $j('#level1_name_new_loginredirect').val($j.trim($j('#redirect_'+id).html()));
               $j('#level_name_new_campaign_name').val($j.trim($j('#campaign_name_'+id).html()));               
               if($j('#post_'+id).html()=='X') $j('#level_name_new_allposts').removeAttr('checked'); 
               else $j('#level_name_new_allposts').attr('checked','checked');
               if($j('#cat_'+id).html()=='X') $j('#level_name_new_allcategories').removeAttr('checked'); 
               else $j('#level_name_new_allcategories').attr('checked','checked');
               if($j('#page_'+id).html()=='X') $j('#level_name_new_allpages').removeAttr('checked');
               else $j('#level_name_new_allpages').attr('checked','checked');
               if($j('#comment_'+id).html()=='X') $j('#level_name_new_allcomments').removeAttr('checked'); 
               else $j('#level_name_new_allcomments').attr('checked','checked');
               var subscription = $j.trim($j('#expiry_'+id).html());
               var etype = $j('#expiry_'+id).attr('etype');
               if(etype=='noexpire'){
                  $j('#noexpire_button_new_level').attr('checked', "checked");
                  $j('#level_name_new_calendar').attr('disabled','disabled');
                  $j('#level_name_new_expire').attr('disabled','disabled');
                  $j('#expire_on_fixed_date_new_level').attr('disabled', 'disabled');
               }
               else if(etype == 'interval'){
                  subscription = subscription.split(' ');
                  $j('#noexpire_button_interval_new_level').attr('checked', 'checked');                  
                  $j('#level_name_new_calendar').removeAttr('disabled').val(subscription[1]);
                  $j('#level_name_new_expire').removeAttr('disabled').val(subscription[0]);
                  $j('#expire_on_fixed_date_new_level').attr('disabled','disabled');
               }
               else if (etype =='fixeddate'){
                  $j("#expire_on_fixed_date_new_level").dateinput({'format':'yyyy-mm-dd',selectors: true,yearRange:[-100,100]});
                  $j('#noexpire_button_fixed_date_new_level').attr('checked', 'checked');
                  $j('#expire_on_fixed_date_new_level').removeAttr('checked').val(subscription);
                  $j('#level_name_new_calendar').attr('disabled','disabled');
                  $j('#level_name_new_expire').attr('disabled','disabled');                  
               }
            });
         }
         else if (label=='Delete')
         {
            $j(this).click(function(){
               top.document.location = 'admin.php?page=eMember_membership_level_menu&delete='+$j(this).attr('id');
               return false;
            });
            $j(this).confirm({msg:"",timeout:5000});
         }
      });
   }
   function showList(){
      $j('#hidden_id').remove();
      var levelList = $j($j('#hidden_content').html()).clone();
      $j('#hidden_content').html('');
      var levelForm = $j($j('#emember_form').slideUp().html()).clone();
      $j('#emember_form').html('');
      $j(levelForm).appendTo('#hidden_content');
      $j('#emember_form').append($j(levelList)).slideDown();
      $j('#add_new').click(showForm);
      checkExpiry('#emember_form');
      bindActions();
   }
   $j(document).ready(function($j){
      available = true;
      bindActions();
      $j('#e_membership_levels').show('slow');
      $j('#add_new').click(showForm);
      checkExpiry('#emember_form');
      $j('#emember_form').submit(function(){
         var ok = true;
         $j(this).find('input[id^=noexpire_button_]').
            each(function(i){
               if($j(this).attr('checked')){
                   var v = 'noexpire';
                   var el = '';
                   if($j(this).attr('id') == 'noexpire_button_interval_new_level'){
                       el = $j('#level_name_new_expire');
                       v = el.val();
                   }                      
                   else if ($j(this).attr('id') == 'noexpire_button_fixed_date_new_level'){
                      el = $j('#expire_on_fixed_date_new_level');  
                      v = el.val();  
                   }
                   
                  if(v==''){
                     el.css("background", "red").
                        focus(function(){$j(this).css('background','')});
                     ok = false;
                  }
               }
            });

            $j(this).find('input[id^="level_name_"]').
               each(function(i){
                  if($j(this).attr('id') == 'level_name_new_campaign_name'){
                      
                  }
                  else if(($j(this).val()=='')&&(!$j(this).attr('disabled')))
                  {
                     $j(this).css('background', 'red');
                     ok = false;
                  }
               });
         return ok&&available;
      });
   });   
</script>